//
//  ZZCSetGestureController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCSetGestureController.h"
#import "ZZCGestureView.h"
#import "ZZCGestureController.h"

@interface ZZCSetGestureController ()<ZZCGestureViewDelegate>
@property (weak,   nonatomic) IBOutlet UILabel *titleLabel;
@property (weak,   nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) ZZCGestureView *gesView;
@property (assign, nonatomic) BOOL isPassword;//判断是否以输密码
@property (copy,   nonatomic) NSString *passwordStr;

@end

@implementation ZZCSetGestureController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isPassword = NO;
}

- (void)viewDidLayoutSubviews {
    self.gesView = [[ZZCGestureView alloc]initWithFrame:self.bgView.bounds];
    self.gesView.delegate = self;
    [self.bgView addSubview:self.self.gesView];
    ZZCLog(@"%@",NSStringFromCGRect(self.bgView.frame));
}

#pragma mark - ZZCGestureViewDelegate

//初次设置密码
- (void)GestureLockSetResult:(NSString *)result gestureView:(ZZCGestureView *)gestureView {
    ZZCLog(@"输入密码：%@",result);
    self.titleLabel.text = @"再次输入密码";
    if (self.isPassword) {
        if ([self.passwordStr isEqualToString:result]) {
            [gestureView setRigthResult:result];
              self.titleLabel.text = @"密码设置成功";
            [self setDump:result];
              [UIAlertController showInfoAlertLoginVC:self title:@"提示" message:@"密码设置成功" ok:^{
                  for (UIViewController *controller in self.navigationController.viewControllers) {
                      if ([controller isKindOfClass:[ZZCGestureController class]]) {
                          [self.navigationController popToViewController:controller animated:YES];
                          [[NSNotificationCenter defaultCenter] postNotificationName:@"backPasswordResult" object:self userInfo:@{@"result":result}];
                      }
                  }
              }];
           
        }else {
            self.titleLabel.text = @"两次密码设置不一样，请重新设置";
            [self performSelector:@selector(resetLabel) withObject:nil afterDelay:1];
            self.passwordStr = nil;
            self.isPassword = NO;
            return;
        }
    }
    self.passwordStr = result;
    self.isPassword = YES;
}

- (void)resetLabel {
    ZZCLog(@"绘制新手势密码");
    self.titleLabel.text = @"绘制新手势密码";
}

- (void)setDump:(NSString *)result {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"hand_password":result};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"hand_password":result};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_DUMP parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
        }
    }];

}

- (IBAction)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
